home *** CD-ROM | disk | FTP | other *** search
/ Speccy ClassiX 1998 / Speccy ClassiX 98.iso / amiga_system / the_aminet / dev / c / asyncio.lha / AsyncIO / src / WaitPacket.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-10  |  2.1 KB  |  73 lines

  1. #include "async.h"
  2.  
  3. /* this function waits for a packet to come back from the file system. If no
  4.  * packet is pending, state from the previous packet is returned. This ensures
  5.  * that once an error occurs, it state is maintained for the rest of the life
  6.  * of the file handle.
  7.  *
  8.  * This function also deals with IO errors, bringing up the needed DOS
  9.  * requesters to let the user retry an operation or cancel it.
  10.  */
  11. LONG
  12. AS_WaitPacket( AsyncFile *file )
  13. {
  14. #ifdef ASIO_NOEXTERNALS
  15.     struct ExecBase        *SysBase;
  16.     struct DosLibrary    *DOSBase;
  17. #endif
  18.  
  19.     LONG bytes;
  20.  
  21. #ifdef ASIO_NOEXTERNALS
  22.     SysBase = file->af_SysBase;
  23.     DOSBase = file->af_DOSBase;
  24. #endif
  25.  
  26.     if( file->af_PacketPending )
  27.     {
  28.         while( TRUE )
  29.         {
  30.             /* This enables signalling when a packet comes back to the port */
  31.             file->af_PacketPort.mp_Flags = PA_SIGNAL;
  32.  
  33.             /* Wait for the packet to come back, and remove it from the message
  34.              * list. Since we know no other packets can come in to the port, we can
  35.              * safely use Remove() instead of GetMsg(). If other packets could come in,
  36.              * we would have to use GetMsg(), which correctly arbitrates access in such
  37.              * a case
  38.              */
  39.             Remove( ( struct Node * ) WaitPort( &file->af_PacketPort ) );
  40.  
  41.             /* set the port type back to PA_IGNORE so we won't be bothered with
  42.              * spurious signals
  43.              */
  44.             file->af_PacketPort.mp_Flags = PA_IGNORE;
  45.  
  46.             /* mark packet as no longer pending since we removed it */
  47.             file->af_PacketPending = FALSE;
  48.  
  49.             bytes = file->af_Packet.sp_Pkt.dp_Res1;
  50.  
  51.             if( bytes >= 0 )
  52.             {
  53.                 /* packet didn't report an error, so bye... */
  54.                 ++file->af_Received;    /* A packet successfully received */
  55.                 return( bytes );
  56.             }
  57.  
  58.             /* see if the user wants to try again... */
  59.             if( ErrorReport( file->af_Packet.sp_Pkt.dp_Res2, REPORT_STREAM, file->af_File, NULL ) )
  60.                 return( -1 );
  61.  
  62.             /* user wants to try again, resend the packet */
  63.             AS_SendPacket( file, file->af_Buffers[ file->af_ReadMode ?
  64.                 file->af_CurrentBuf : 1 - file->af_CurrentBuf ] );
  65.         }
  66.     }
  67.  
  68.     /* last packet's error code, or 0 if packet was never sent */
  69.     SetIoErr( file->af_Packet.sp_Pkt.dp_Res2 );
  70.  
  71.     return( file->af_Packet.sp_Pkt.dp_Res1 );
  72. }
  73.